/**
 * Copyright (c) 2016 coderyuan.com. All Rights Reserved.
 *
 * EShop
 *
 * RedisUtils.java created on 下午1:56
 *
 * @author yuanguozheng
 * @version 1.0.0
 * @since 16/3/4
 */
package com.coderyuan.web.api.kit.database;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.coderyuan.web.api.kit.utils.JsonUtil;

import redis.clients.jedis.Jedis;

/**
 * RedisUtils
 *
 * @author yuanguozheng
 */
public class RedisManager {

    private static final Logger LOGGER = LoggerFactory.getLogger(RedisManager.class);

    private static final RedisManager INSTANCE = new RedisManager();

    private static Jedis sJedisClient;

    public static void init(String ip, int port) {
        sJedisClient = new Jedis(ip, port);
    }

    public static RedisManager getInstance() {
        return INSTANCE;
    }

    public void insertJson(String key, Object obj) {
        insert(key, JsonUtil.toJson(obj));
    }

    public void insert(String key, String value) {
        try {
            String result = sJedisClient.set(key, value);
            LOGGER.debug(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String get(String key) {
        try {
            return sJedisClient.get(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void delete(String key) {
        try {
            long i = sJedisClient.del(key);
            LOGGER.debug(i + "");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
